<template>
  <div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="false"
      :loading="loading"
      :row-selection="rowSelection"
      style="height: 300px; overflow-x: hidden; overflow-y: auto"
    >
    </a-table>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import { getAction } from '@/api/manage'

export default {
  name: 'left',
  mixins: [JeecgListMixin],
  components: {
    // SearchModel
  },
  props: ['value'],
  data() {
    return {
      queryParam: {},
      // 表头
      columns: columns(this),
      url: {
        list: '/sjyzzx/sjZxzyAssistAccount/querySubjectListByAssistCode',
      },
      disableMixinCreated: true,
      reportIds: [],
      defulatSelectedRowKeyAll: [],
      loading: false,
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(assistCode) {
        // debugger
        this.queryParam.assistCode = assistCode
        this.dataSource = []
        this.reportIds = []
        this.defulatSelectedRowKeyAll = []
        this.loadData(1)
      },
    },
  },
  created() {},
  mounted() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.defulatSelectedRowKeyAll,
        onChange: (selectedRowKeys, selectedRows) => {
          this.reportIds = []
          this.defulatSelectedRowKeyAll = [...selectedRowKeys]
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
          selectedRows.map((item) => {
            this.reportIds.push({ subjectCode: item.subjectCode, subjectName: item.subjectName })
          })
          this.$emit('onSelectSubject', this.reportIds)
        },
      }
    },
  },
  methods: {
    updateList() {
      this.loadData()
    },
    clear() {
      this.defulatSelectedRowKeyAll = []
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() // 查询条件
      this.loading = true
      this.reportIds = []
      this.defulatSelectedRowKeyAll = []
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          // update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (this.value == 'b05_8B3D3132-1F56-4BAC-837C-F2664E63E92F') {
            this.dataSource.map((item) => {
              this.reportIds.push({ subjectCode: item.subjectCode, subjectName: item.subjectName })
              this.defulatSelectedRowKeyAll.push(item.id)
            })
          }
          this.$emit('onSelectSubject', this.reportIds)
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          // update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>